package com.newbsd.web.maventest.controller;

import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.newbsd.web.maventest.dao.impl.UserDaoImpl;
import com.newbsd.web.maventest.pojo.User;

/**
 * Servlet implementation class UserAddServlet
 */
@WebServlet("/UserAddServlet")
public class UserAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserAddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		
		UserDaoImpl udi = new UserDaoImpl();
		
		int userid = Integer.parseInt(request.getParameter("userid"));

		String username = request.getParameter("username");
		//username = new String(username.getBytes("ISO-8859-1"), "UTF-8");
		String password = request.getParameter("password");
		String realname = request.getParameter("realname");
		String sex = request.getParameter("sex");
		String certtype = request.getParameter("certtype");
		String cert = request.getParameter("cert");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		//Date birthday = (Date)request.getParameter("birthday");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date birthday = null;
		try {
			birthday = (Date) df.parse((String)request.getParameter("birthday"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//birthday = df.parse((String)request.getParameter("birthday"));
			
		String usertype = request.getParameter("usertype");
		String content = request.getParameter("content");

		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setRealname(realname);
		user.setSex(sex);
		user.setCerttype(certtype);
		user.setCert(cert);
		user.setProvince(province);
		user.setCity(city);
		user.setBirthday(birthday);
		user.setUsertype(usertype);
		user.setContent(content);


		// 增加操作
		if (null == username) {
//			userInfoDao.addUserInfo(info);
//
//			if (null != oper && !"".equals(oper)) {
//				response.sendRedirect("/bookinfo/user/query");
//			} else {
//				response.sendRedirect("/bookinfo/view/login.jsp");
//			}
		}
		// 修改操作
		else {
			user.setUserid(userid);;
//			User one = udi.getUser(username);
			//int id = one.getUserid();
//			user.setUserid(userid);
			//user.setId(id);
			//int id = Integer.parseInt(request.getParameter("id"));
			
			udi.updateUserInfo(user);
			//response.sendRedirect("/maventest/UserQueryServlet");
			request.getRequestDispatcher("/UserQueryServlet").forward(request, response);
		}

	}

}
